package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.content.Intent;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.pickerview.TimePickerView;
import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemLongClickListener;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.ZHCXEmergencyCuring1ListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.Curing1Bean;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.DateUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

import static patrol.mobile.com.mobilepatrol.utils.DateUtil.format4;

/**
 * 综合查询
 */
public class ZHCXEmergencyCuring1Activity extends CommonListActivity implements CallBack<DateBean> {

    private View itemTopView;
    private MySpinner spOrgName;
    private MySpinner spMonth;
    private ImageView ivSearch;

    private List<Curing1Bean> protectCollectionBeanList = new ArrayList<>();

    private String orgId = "";
    private String month = "";
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    private List<String> orgNameList = new ArrayList<>();
    private List<String> orgIdList = new ArrayList<>();

    @Override
    public void initView() {
        super.initView();

        itemTopView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_curing1_list_top, null);
        spOrgName = itemTopView.findViewById(R.id.spOrgName);
        spMonth = itemTopView.findViewById(R.id.spMonth);
        ivSearch = itemTopView.findViewById(R.id.ivSearch);
        flContent.addView(itemTopView);

        spOrgName.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
        spMonth.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
    }


    @Override
    public void initTitle() {
        super.initTitle();
        iv_right_title.setVisibility(View.GONE);
        iv_right_title.setImageDrawable(getResources().getDrawable(R.mipmap.jz));

        iv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ZHCXEmergencyCuring1Activity.this, EmergencyCuringCheck3Activity.class);
                if (protectCollectionBeanList != null && protectCollectionBeanList.size() > 0) {//加号默认使用当前列表的第一个item的值跳转
                    intent.putExtra("id", protectCollectionBeanList.get(0).getId());
                    startActivity(intent);
                }

            }
        });
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new ZHCXEmergencyCuring1ListAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_CURING_CHECK_1;
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
        getOrg();
    }

    @Override
    public void setListener() {
        super.setListener();
//        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//                startActivity(EmergencyCuring2Activity.class);
//            }
//        });

        Calendar cd = Calendar.getInstance();
        month = (cd.get(Calendar.MONTH) + 1) + "";
        spMonth.getmText().setText(sdf.format(new Date()));
//        spOrgName.seton;
        spMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //时间选择器
                TimePickerView pvTime = new TimePickerView.Builder(ZHCXEmergencyCuring1Activity.this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        month = DateUtil.convert2String(date, format4);
                        spMonth.setText(month);
                        requestData();
                    }
                }).setType(new boolean[]{true, true, false, false, false, false}).setLabel("年", "月", null, null, null, null).build();
                pvTime.show();
            }
        });
        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String orgNme = spOrgName.getmText().getText().toString();
                String date = spMonth.getmText().getText().toString();
                if (date.length() > 8) {
                    month = date.substring(0, 7);
                }
                for (int i = 0; i < orgNameList.size(); i++) {
                    if (orgNme.equals(orgNameList.get(i))) {
                        orgId = orgIdList.get(i);
                        requestData();
                        return;
                    }
                }

            }
        });


        mAdapter.setOnItemLongClickListener(new OnRecyclerViewItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View view, int position) {
                //长按删除
                Curing1Bean protectCollectionBean = protectCollectionBeanList.get(position);

                deleteData(protectCollectionBean.getId());

                return false;
            }
        });

    }

    private void deleteData(String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_CURING_1_DELETE);
        sender.requestPost("id", id, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                requestData();
//                String orgNme = spOrgName.getmText().getText().toString();
//                String date = spMonth.getmText().getText().toString();
//                if (date.length() > 8) {
//                    month = date.substring(0, 7);
//                }
//                for (int i = 0; i < orgNameList.size(); i++) {
//                    if (orgNme.equals(orgNameList.get(i))) {
//                        orgId = orgIdList.get(i);
//                        requestData();
//                        return;
//                    }
//                }
            }
        });
    }


    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
                orgNameList.clear();
                orgIdList.clear();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    orgNameList.add(listBean.getOrgName());
                    orgIdList.add(listBean.getId());
                }
                if (orgNameList != null && orgNameList.size() > 0) {
                    spOrgName.getmText().setText(orgNameList.get(0));
                    orgId = orgListBean.getList().get(0).getId();
                }

                requestData();

                spOrgName.setmDatas(orgNameList, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        orgId = orgListBean.getList().get(postion).getId();
                        requestData();
                    }
                });
            }
        });
    }


    private void requestData() {
//        String orgNme = spOrgName.getmText().getText().toString();
        month = spMonth.getmText().getText().toString();
//        if (date.length() > 8) {
//            month = date.substring(0, 7);
//        }
//        for (int i = 0; i < orgNameList.size(); i++) {
//            if (orgNme.equals(orgNameList.get(i))) {
//                orgId = orgIdList.get(i);
//                // return;
//            }
//        }


        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_CURING_1);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("month", month);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                protectCollectionBeanList = GsonUtil.jsonToList(listArray.toString(), Curing1Bean.class);
                mAdapter.updateItems(protectCollectionBeanList);
            }
        });
    }


    @Override
    public void onBack(DateBean dateBean) {


    }

}
